import dayjs from 'dayjs';
/**
 * 时间转换
 */
export const timeDictionary = (_data: number, _format: string = 'YYYY-MM-DD HH:mm:ss') => {
	return _data ? dayjs(_data * 1000).format(_format) : '----';
};

/**
 * 将科学计数法的数字转为字符串
 * 说明：运算精度丢失方法中处理数字的时候，如果出现科学计数法，就会导致结果出错
 * 4.496794759834739e-9  ==> 0.000000004496794759834739
 * 4.496794759834739e+9  ==> 4496794759.834739
 * @param  num
 */
export const toNonExponential = (num: any) => {
	if (num == null) {
		return num;
	}
	if (typeof num == 'number') {
		const m: any = num.toExponential().match(/\d(?:\.(\d*))?e([+-]\d+)/);
		return num.toFixed(Math.max(0, (m[1] || '').length - m[2]));
	} else {
		return num;
	}
};

/**
 * 乘法 - js运算精度丢失问题
 * @param arg1  数1
 * @param arg2  数2
 * 0.0023 * 100 ==> 0.22999999999999998
 * {{ 0.0023 | multiply(100) }} ==> 0.23
 */
export const floatMultiply: any = (arg1: any, arg2: any) => {
	arg1 = Number(arg1);
	arg2 = Number(arg2);
	if ((!arg1 && arg1 !== 0) || (!arg2 && arg2 !== 0)) {
		return null;
	}
	arg1 = toNonExponential(arg1);
	arg2 = toNonExponential(arg2);
	let n1, n2;
	let r1, r2; // 小数位数
	try {
		r1 = arg1.toString().split('.')[1].length;
	} catch (e) {
		r1 = 0;
	}
	try {
		r2 = arg2.toString().split('.')[1].length;
	} catch (e) {
		r2 = 0;
	}
	n1 = Number(arg1.toString().replace('.', ''));
	n2 = Number(arg2.toString().replace('.', ''));
	return (n1 * n2) / Math.pow(10, r1 + r2);
};

/**
 * 除法 - js运算精度丢失问题
 * @param arg1  数1
 * @param arg2  数2
 * 0.0023 / 0.00001 ==> 229.99999999999997
 * {{ 0.0023 | divide(0.00001) }} ==> 230
 */
export const floatDivide = (arg1: any, arg2: any) => {
	arg1 = Number(arg1);
	arg2 = Number(arg2);
	if (!arg2) {
		return null;
	}
	if (!arg1 && arg1 !== 0) {
		return null;
	} else if (arg1 === 0) {
		return 0;
	}
	arg1 = toNonExponential(arg1);
	arg2 = toNonExponential(arg2);
	let n1, n2;
	let r1, r2; // 小数位数
	try {
		r1 = arg1.toString().split('.')[1].length;
	} catch (e) {
		r1 = 0;
	}
	try {
		r2 = arg2.toString().split('.')[1].length;
	} catch (e) {
		r2 = 0;
	}
	n1 = Number(arg1.toString().replace('.', ''));
	n2 = Number(arg2.toString().replace('.', ''));
	return floatMultiply(n1 / n2, Math.pow(10, r2 - r1));
	// return (n1 / n2) * Math.pow(10, r2 - r1);   // 直接乘法还是会出现精度问题
};

/**
 * 加法 - js运算精度丢失问题
 * @param arg1  数1
 * @param arg2  数2
 * 0.0023 + 0.00000000000001 ==> 0.0023000000000099998
 * {{ 0.0023 | plus(0.00000000000001) }} ==> 0.00230000000001
 */
export const floatAdd = (arg1: any, arg2: any) => {
	arg1 = Number(arg1) || 0;
	arg2 = Number(arg2) || 0;
	arg1 = toNonExponential(arg1);
	arg2 = toNonExponential(arg2);
	let r1, r2, m;
	try {
		r1 = arg1.toString().split('.')[1].length;
	} catch (e) {
		r1 = 0;
	}
	try {
		r2 = arg2.toString().split('.')[1].length;
	} catch (e) {
		r2 = 0;
	}
	m = Math.pow(10, Math.max(r1, r2));
	return (floatMultiply(arg1, m) + floatMultiply(arg2, m)) / m;
};

/**
 * 减法 - js运算精度丢失问题
 * @param arg1  数1
 * @param arg2  数2
 * 0.0023 - 0.00000011  ==>  0.0022998899999999997
 * {{ 0.0023 | minus( 0.00000011 ) }}  ==>  0.00229989
 */
export const floatSub = (arg1: any, arg2: any) => {
	arg1 = Number(arg1) || 0;
	arg2 = Number(arg2) || 0;
	arg1 = toNonExponential(arg1);
	arg2 = toNonExponential(arg2);
	let r1, r2, m, n;
	try {
		r1 = arg1.toString().split('.')[1].length;
	} catch (e) {
		r1 = 0;
	}
	try {
		r2 = arg2.toString().split('.')[1].length;
	} catch (e) {
		r2 = 0;
	}
	m = Math.pow(10, Math.max(r1, r2));
	// 动态控制精度长度
	n = r1 >= r2 ? r1 : r2;
	return ((floatMultiply(arg1, m) - floatMultiply(arg2, m)) / m).toFixed(n);
};

/**
 * 取余 - js运算精度丢失问题
 * @param arg1  数1
 * @param arg2  数2
 * 12.24 % 12  ==> 0.2400000000000002
 * {{ 12.24 | mod( -12 ) }}  ==>  0.24
 */
export const floatMod = (arg1: any, arg2: any) => {
	arg1 = Number(arg1);
	arg2 = Number(arg2);
	if (!arg2) {
		return null;
	}
	if (!arg1 && arg1 !== 0) {
		return null;
	} else if (arg1 === 0) {
		return 0;
	}
	let intNum = arg1 / arg2;
	intNum = intNum < 0 ? Math.ceil(arg1 / arg2) : Math.floor(arg1 / arg2); // -1.02 取整为 -1; 1.02取整为1
	let intVal = floatMultiply(intNum, arg2);
	return floatSub(arg1, intVal);
};

/**
 * 订单状态
 */
export const orderStatusText = (val: any) => {
	switch (Number(val)) {
		case -1:
			return '已关闭';
		case 0:
			return '已取消';
		case 1:
			return '待付款';
		case 2:
			return '待发货';
		case 3:
			return '备货中';
		case 4:
			return '待收货';
		case 5:
			return '已完成';
		default:
			return '未知状态';
	}
};
/**
 * 订单退款状态
 */
export const refundStatusText = (val: any) => {
	switch (Number(val)) {
		case 0:
			return '未申请退款';
		case 1:
			return '退款待审核';
		case -1:
			return '退款已取消';
		case 2:
			return '打款中';
		case 3:
			return '审核失败';
		case 4:
			return '打款失败';
		case 5:
			return '退款成功';
		default:
			return '未知状态';
	}
};
